package com.pencilisland.pay.mgt.app.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pencilisland.pay.mgt.app.entity.TenantEntity;
import com.pencilisland.pay.mgt.app.service.TenantService;

@Controller
@RequestMapping(value = "/app/tenant")
public class TenantController {
	@Autowired
	TenantService tenantService;

	/**
	 * 进入商户管理页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "")
	public String toTenantList() {
		return "app/tenant";
	}

	@RequestMapping(value = "/list")
	@ResponseBody
	public Map<String, Object> tenantList(@ModelAttribute TenantEntity cretiria,
			@RequestParam(value = "start") int start, @RequestParam(value = "length") int length) {
		Map<String, Object> resultMap = new HashMap<String, Object>();

		Page<TenantEntity> tenantPage = tenantService.tenantList(cretiria, start, length);

		resultMap.put("recordsFiltered", tenantPage.getTotalElements());
		resultMap.put("recordsTotal", tenantPage.getTotalElements());
		resultMap.put("tenantList", tenantPage.getContent());

		return resultMap;
	}

	/**
	 * 保存更新
	 * 
	 * @param org
	 * @return
	 */
	@RequestMapping(value = "/save")
	@ResponseBody
	public TenantEntity save(@ModelAttribute TenantEntity tenant) {
		return tenantService.save(tenant);
	}

	/**
	 * 加载数据
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/load/{id}")
	@ResponseBody
	public Map<String, Object> load(@PathVariable(value = "id") Long id) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("success", true);
		try {
			resultMap.put("data", tenantService.loadData(id));
		} catch (Exception e) {
			e.printStackTrace();
			resultMap.put("success", false);
		}
		return resultMap;
	}
	
	/**
	 * 加载数据
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/session")
	@ResponseBody
	public Map<String, Object> sessionData() {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("success", true);
		try {
			resultMap.put("data", tenantService.sessionData());
		} catch (Exception e) {
			e.printStackTrace();
			resultMap.put("success", false);
		}
		return resultMap;
	}

	/**
	 * 删除数据
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/delete/{id}")
	@ResponseBody
	public Map<String, Object> delete(@PathVariable(value = "id") Long id) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("success", true);
		try {
			tenantService.delete(id);
		} catch (Exception e) {
			e.printStackTrace();
			resultMap.put("success", false);
		}
		return resultMap;
	}
}
